﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Linq.Mapping;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(IOrder))]
    public partial class Order:Entity,IOrder
    {

     
        OrderStatus IOrder.OrderStatus
        {
            get
            {
                return (OrderStatus)this.OrderStatus;
            }
            set
            {
                this.OrderStatus = (int)value;
            }
        }


        List<IOrderItem> _Items;
        List<IOrderItem> IOrder.OrderItems
        {
            get
            {
                if (this._Items == null)
                {
                    this._Items = this.DataContext.OrderItems
                        .Where(i => i.OrderId == this.Id)
                        .OfType<IOrderItem>()
                        .ToList();
                }

                return this._Items;
            }
            set
            {
                this._Items = value;
            }
        }


        ICustomer IOrder.Customer
        {
            get
            {
                if (this.Customer == null)
                {
                    this.Customer = this.DataContext.Customers
                        .Where(i => i.Id == this.CustomerId)
                        .FirstOrDefault();
                }

                return this.Customer;
            }
            set
            {
                this.Customer = value as Customer;

                if (value == null)
                {
                    this.CustomerId = null;
                }
                else
                {
                    this.CustomerId = value.Id;
                }
            }
        }


        ICart IOrder.Cart
        {
            get
            {
                return this.Cart;
            }
            set
            {
                if (value == null)
                {
                    this.CartId = null;
                }
                else
                {
                    this.CartId = value.Id;
                }
            }
        }




        List<IPromotionRule> _Promotions;
        public List<IPromotionRule> Promotions
        {
            get
            {
                if (this._Promotions == null)
                {
                    this._Promotions = new List<IPromotionRule>();
                }
                return this._Promotions;
            }
        }



        private String shippingName;
        public string ShippingName
        {
            get {
                if (String.IsNullOrEmpty(shippingName))
                {
                    shippingName = this.DataContext.Shippings.Where(s => s.Id == ShippingId).FirstOrDefault().Name;
                }
                return shippingName;
            }
        }
    }
}
